package org.kl.bf.utils;

import java.io.IOException;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

public class EmailUtil {

	/**
	 * smtp host 不唯一 smtp.163.com smtp.21cn.com smtp.sina.com.cn smtp.sohu.com
	 * smtp.126.com
	 */
	public static String PROTOCOL = "";
	public static String FROM = "";
	public static String PWD = "";

	// 完成三个基本变量的获取
	private static void getEmailMessage() {
		Resource resource = new ClassPathResource("/application.properties");
		try {
			Properties props = PropertiesLoaderUtils.loadProperties(resource);
			if (props != null) {
				PROTOCOL = (String) props.get(BasicConstants.PROTOCOL);
				FROM = (String) props.get(BasicConstants.FROM);
				PWD = (String) props.get(BasicConstants.PWD);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取Session
	 * 
	 * @return
	 */
	private static Session getSession() {
		getEmailMessage();
		// 通过邮箱地址解析出smtp服务器，对大多数邮箱都管用
		String hostName = "smtp." + FROM.split("@")[1];

		Properties props = new Properties();
		props.put("mail.smtp.host", hostName);// 设置服务器地址
		props.put("mail.store.protocol", PROTOCOL);// 设置协议
		props.put("mail.smtp.auth", "true");
		if (hostName.contains("qq.com")) {
			props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
			props.put("mail.smtp.port", "465");
			props.put("mail.smtp.socketFactory.port", "465");
		} else {
			props.put("mail.smtp.port", "25");// 设置端口
		}

		Authenticator authenticator = new Authenticator() {

			@Override
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(FROM, PWD);
			}

		};
		Session session = Session.getDefaultInstance(props, authenticator);
		// session.setDebug(true); // 为了方便调试

		return session;
	}

	public static void send(String toEmail, String content, int judg) {
		Session session = getSession();
		try {
			// Instantiate a message
			Message msg = new MimeMessage(session);

			// Set message attributes
			msg.setFrom(new InternetAddress(FROM));
			InternetAddress[] address = { new InternetAddress(toEmail) };
			msg.setRecipients(Message.RecipientType.TO, address);
			if (judg == 0) {
				msg.setSubject("广东省核工业华南高级技工学校智慧校园平台邮箱激活邮件");
			} else {
				msg.setSubject("广东省核工业华南高级技工学校智慧校园平台找回密码邮件");
			}
			msg.setSentDate(new Date());
			msg.setContent(content, "text/html;charset=utf-8");

			// Send the message
			Transport.send(msg);
		} catch (MessagingException mex) {
			mex.printStackTrace();
		}
	}
}
